//
//  CHPhotosView.m
//  CarFile
//
//  Created by 陈浩 on 2017/8/6.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHPhotosView.h"
#import "CHPhotoImgView.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
#define MaxImageCount 6 //最大的图片个数
#define Kcol 3
#define Krow 2
// 图片宽度和高度

#define imgViewH 100
// 图片的间距
#define imgMargin 10
@implementation CHPhotosView

-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        // 初始化9个图片
        for (NSInteger i = 0; i < MaxImageCount;i++) {
            UIImageView *photoView = [[UIImageView alloc] init];
            photoView.tag = self.subviews.count;
            photoView.userInteractionEnabled = YES;
            // 在imgView添加 gif标识
            [self addSubview:photoView];
//             监听图片点击手势
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoViewTap:)];
            [photoView addGestureRecognizer:tap];
        }
    }

    return self;
}

#pragma mark 图片点击触发的方法
-(void)photoViewTap:(UITapGestureRecognizer *)tap{

#warning 此图片浏览器不要使用懒加载
    MJPhotoBrowser  *browser = [[MJPhotoBrowser alloc] init];


    //设置图片数据源
    NSMutableArray *photos = [NSMutableArray array];

    NSInteger picCount = self.pic_urls.count;
    for (NSInteger i = 0;i < picCount; i++) {
        NSString *urlStr = self.pic_urls[i];
        MJPhoto *mjPhoto = [[MJPhoto alloc] init];

        //设置大图片路径
        mjPhoto.url = [NSURL URLWithString:urlStr];

        //设置srcImgView
        mjPhoto.srcImageView = self.subviews[i];

        mjPhoto.index = (int)i;

        [photos addObject:mjPhoto];
    }



    // 设置显示图片索引
    browser.currentPhotoIndex = tap.view.tag;

    browser.photos = photos;
    //显示图片游览器
    [browser show];

}

-(void)setPic_urls:(NSArray *)pic_urls{
    _pic_urls = pic_urls;

    

    // 下载并显示图片
    NSInteger picCount = pic_urls.count;

    for (NSInteger i = 0 ; i < picCount;i++) {
        UIImageView *photoView = self.subviews[i];

        NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *str = [[NSString alloc]initWithString:[pic_urls[i] stringByTrimmingCharactersInSet:whiteSpace]];
        //去汉字
//        NSString *tempString = [pic_urls[i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//        //去汉字和替换空格处理
//        NSString *tempString2 = [[NSString stringWithFormat:@"%@",tempString]
////                                 stringByReplacingOccurrencesOfString:@" " withString:@"%20 "];
//        NSURL *url =[NSURL URLWithString:tempString2];
        
        [photoView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"placeholder"]];
 
    }



// 隐藏多余图片
    for (NSInteger i = 0; i < MaxImageCount;i++) {
        UIImageView *imgView = self.subviews[i];
        
        imgView.hidden = (i >= picCount);

    }

    [self setNeedsLayout];
}

-(void)layoutSubviews{
    [super layoutSubviews];

    // 图片个数
    NSInteger picCount = self.pic_urls.count;


    // 获取最大列数
    CGFloat imgViewW =(self.w - 20)/3;
    for (NSInteger i = 0;i < picCount;i++) {

        UIImageView *imgView = self.subviews[i];

        // 2.获取当前图片所处行数
        NSInteger col = i % Kcol;
        NSInteger row = i / Kcol;

        CGFloat imgX =   col *(imgViewW + imgMargin);
        CGFloat imgY =   row *(imgViewH +imgMargin);

        imgView.frame = CGRectMake(imgX, imgY, imgViewW, imgViewH);
        
    }


}

//+(CGSize)sizeWithPhotosCount:(NSInteger)count{
//
////    // 如果没有图片，图片size就是0
////    if (count == 0) {
////        return CGSizeZero;
////    }
////
////
////    // 有图片，根据个数，计算photosView的size
////    // 图片显示最大列数
////    /*
////     * 如果有4张图片，显示2列，其它情况显示3列
////     */
////    NSInteger maxColumn = count >3 ? 2 : 1;
////
////    // 图片总行数
////    NSInteger totalRow = count / maxColumn;
////
////    if (count % maxColumn != 0) {
////        totalRow +=1;
////    }
////
////    // photosView的宽高
////    CGFloat photosViewW = maxColumn * imgViewW + (maxColumn - 1) * imgMargin;
////    CGFloat photosViewH = totalRow * imgViewH + (totalRow - 1) * imgMargin;
////    NSLog(@"图片显示列数 %ld 行数 %ld",maxColumn,totalRow);
////    return CGSizeMake(photosViewW, photosViewH);
//
//}

@end
